package com.zhugang.stack;


/**
 * @program algorithms
 * @description: 基于链表实现的栈
 * @author: chanzhugang
 * @create: 2022/02/22 00:32
 */
public class StackOnLinkedList {

    private Node top;

    /**
     * 入栈
     *
     * @param data
     */
    public void push(String data) {
       /* Node node = new Node(data, null);
        if (top == null) {
            top = node;
        } else {
            node.next = top;
            top = node;
        }*/

        Node node = new Node(data, top);
        top = node;
    }

    /**
     * 出栈
     *
     * @return
     */
    public String pop() {
        if (top == null) {
            return null;
        }
        String data = top.data;
        top = top.next;
        return data;
    }


    static class Node {
        private final String data;
        private final Node next;

        public Node(String data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
}